package com.jackie.jedis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by jackie on 2017/2/27.
 */
public class RedisFactory {

    private static JedisPool jedisPool = null;

    private RedisFactory(){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(10);
        config.setMaxIdle(5);
        config.setMinIdle(2);
        //在borrow一个jedis实例时，是否提前进行validate操作；如果为true，则得到的jedis实例均是可用的；
        config.setTestOnBorrow(true);
        jedisPool = new JedisPool(config,"localhost");
    }

    private static final RedisFactory redisFactory = new RedisFactory();

    public static RedisFactory getRedisFactory(){
        return redisFactory;
    }

    public static Jedis getJedis(){
        return jedisPool.getResource();
    }
    public static void releaseJedis(Jedis jedis){
        jedisPool.returnResource(jedis);
    }

    public static void shutdown(){
        jedisPool.close();
    }

}
